/******************************************************************************
 * @File name   :      UserShortcutMenuController.java
 *
 * @Author      :      chen shixiang
 *
 * @Date        :      2011-6-23
 *
 * @Copyright Notice: 
 * Copyright (c) 2014 DMS, Inc. All  Rights Reserved.
 * This software is published under the terms of the DMS Software
 * License version 1.0, a copy of which has been included with this
 * distribution in the LICENSE.txt file.
 * 
 * 
 * ----------------------------------------------------------------------------
 * Date                   Who         Version        Comments
 * 2011-6-23 上午10:06:07        chen shixiang     1.0            Initial Version
 * 2011-9-16 2:40:07PM        HeHao     1.1            Change Method configUserShortcutMenu
 * 2011-11-7 下午04:27:29         Wu Jianxi       1.1         fix bug
 * 2011-11-30 上午08:38:48      caijunyi        1.1            fix shortcut menu bug
 *****************************************************************************/
package com.cap.bts.framework.ge.menu.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.cap.bts.commonfunc.ge.shortcutmenu.service.IUserShortcutMenuService;
import com.cap.bts.framework.common.controller.BaseController;
import com.cap.bts.framework.common.exception.AppException;
import com.cap.bts.framework.common.util.Utils;
import com.cap.bts.framework.menu.domain.Menu;
import com.cap.bts.framework.menu.service.IMenuService;
import com.cap.bts.framework.tree.Node;
import com.cap.bts.framework.usermgnt.domain.User;

/**
 *
 */
@Controller
@RequestMapping(value="/ge/shortcutmenu")
public class UserShortcutMenuController extends BaseController {

    @Autowired
    IMenuService menuService;
    @Autowired
    IUserShortcutMenuService userShortcutMenuService;
    /**
     * into menu set
     * @Date        :      2011-6-23
     * @return ModelAndView
     * @throws AppException 
     */
    @RequestMapping(value="/config",method = RequestMethod.GET)
    public ModelAndView configUserShortcutMenu(HttpServletRequest request) throws AppException{
         String userMenu="";
        User user = (User) request.getSession().getAttribute("userSession");
        
        List<Menu> shortcutMenu = menuService.getUserShortcutMenusByUserId(user.getId());
        List<String> hasshortMenu = new ArrayList<String>();
        for (Menu menu : shortcutMenu) {
            hasshortMenu.add(menu.getId()+"");
            String parentMenuCode = menu.getParentMenuCode();
            String parentMenuName = "";
            if(Utils.notEmpty(parentMenuCode)){
                Menu parentMenu = menuService.getMenuByMenuCode(parentMenuCode);
                if(parentMenu != null){
                    parentMenuName = parentMenu.getMenuName();
                }
            }
            
            if(null != menu.getBaseUrlType()){
                String site = menuService.getAbsoluteUrlWithMenu(menu);
                userMenu+="<a href="+site+"/"+menu.getUrl()+" target='dms_inner' class='shortcut' parentMenuName='" + parentMenuName + "' secondMenuName ='" + menu.getMenuName() + "'><img src="+site+"/images/"+menu.getPhoneUrl()+" /><div style='width:68px;overflow:hidden;height:14px; height:12px\\9;' title='" + menu.getMenuName() + "'><span>"+menu.getMenuName()+"</span></div></a><div class='line'></div>";
            }else{
                userMenu+="<a href="+menu.getUrl()+"  target='dms_inner' class='shortcut' parentMenuName='" + parentMenuName + "' secondMenuName ='" + menu.getMenuName() + "'><img src="+request.getContextPath()+"/images/"+menu.getPhoneUrl()+" /><div style='width:68px;overflow:hidden;height:14px; height:12px\\9;' title='" + menu.getMenuName() + "'><span>"+menu.getMenuName()+"</span></div></a><div class='line'></div>";
            }
        }
//        int size = hasshortMenu.size();
//        if(size<6){
//            for(int i=size;i<6;i++){
//                userMenu+="<img src="+request.getContextPath()+"/images/logo/tag_peixun.jpg /><span>"+CodeTextUtils.getText("auto", 1)+"</span><div class='line'></div>";
//            }
//        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("shortMenu", hasshortMenu);
        map.put("userMenu", userMenu);
        return new ModelAndView("/ge/menuset/config_shortcut_menu",map);
    }
    
    /**
     * create user menu tree
     * @Date        :      2011-6-28
     * @param request
     * @return
     */
    @RequestMapping(value="/querygrantedmenucodes",method = RequestMethod.POST)
    @ResponseBody
    public List<Node> queryGrantedMenuCodes(HttpServletRequest request){
        User user = (User) request.getSession().getAttribute("userSession");
        List<Node> listNode = menuService.getGrantedMenuNodesByUserCode(user.getUserCode());
        
        return listNode;
    }
    
    
    /**
     * update user shortcut
     * @Date        :      2011-6-28
     * @param menuIds menu list
     * @param request request
     * @return String
     * @throws AppException AppException
     */
    @RequestMapping(value="/updateUserMenu/{menuIds}",method = RequestMethod.GET)
    public String saveOrUpdateUserShortcutMenus(@PathVariable("menuIds") List<String> menuIds,HttpServletRequest request) throws AppException{
        User user = (User) request.getSession().getAttribute("userSession");
        List<Long> menuLong = new ArrayList<Long>();
        for (String long1 : menuIds) {
        	if(long1!=null && long1.equals("-1"))
        	{
        		continue;
        	}
            menuLong.add(Long.parseLong(long1));
        }
        userShortcutMenuService.saveOrUpdateUserShortcutMenus(user.getId(), menuLong);
        return "redirect:/ge/shortcutmenu/config";
    }
    
    /**
     * after save,load user shortcut menu
     * @Date        :      2011-6-28
     * @param request request
     * @return List<Menu>
     */
    @RequestMapping(value="/loadUserShortcutMenus",method = RequestMethod.GET)
    @ResponseBody
    public List<Menu> loadUserShortcutMenus(HttpServletRequest request){
        User user = (User) request.getSession().getAttribute("userSession");
        return menuService.getUserShortcutMenusByUserId(user.getId());
    }
}
